package com.customization.unionpay.market.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.customization.unionpay.market.service.ExternalFundingActivityService;
import com.customization.unionpay.market.util.FormDataUtils;
import com.customization.unionpay.market.util.PropUtils;
import com.engine.core.impl.Service;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;

import java.util.List;

public class ExternalFundingActivityServiceImpl extends Service implements ExternalFundingActivityService {

    private static final Logger log = LoggerFactory.getLogger(ExternalFundingActivityServiceImpl.class);

    @Override
    public JSONObject externalGetInfo(String actAuthCode, String source) throws Exception {

        RecordSet rs = new RecordSet();
        JSONObject result = new JSONObject();
        try {
            if("创新结算系统".equals(source)){
                // 外部出资活动台账表单id
                int formId = Integer.parseInt(PropUtils.BB_LOG.getPropValue("marketSubject", "external_funding_activity_form_id"));
                // 获取外部出资活动台账主表表名
                String maintablename = FormDataUtils.getTableName(formId);
                log.info("获取外部出资活动台账主表表名："+maintablename);
                // 根据当前信息使用id修改新建数据
                String sql = "select gysmc as SUPPLIER,kczjewb as RECHARGEABLE_AT from #{tableName} where yxpthdsqm = ? ";
                sql = sql.replace("#{tableName}",maintablename);
                log.info("sql:"+sql);
                rs.executeQuery(sql,actAuthCode);
                if (rs.next()) {
                    String SUPPLIER = Util.null2String(rs.getString("SUPPLIER"));
                    String RECHARGEABLE_AT = Util.null2String(rs.getString("RECHARGEABLE_AT"));
                    result.put("SUPPLIER", SUPPLIER);
                    result.put("RECHARGEABLE_AT", RECHARGEABLE_AT);
                }else {
                    result.put("SUPPLIER", "");
                    result.put("RECHARGEABLE_AT", "");
                }
            }else if("营销项目管理平台".equals(source) || "MDP".equals(source)){
                // 营销活动事项台账表单id
                int formId = Integer.parseInt(PropUtils.BB_LOG.getPropValue("marketSubject", "marketing_activity_ledger_form_id"));
                // 营销活动事项台账主表表名
                String ledgerTableName = FormDataUtils.getTableName(formId);
                String sql = "select hdnbsyje as APPROVED_AT from #{tableName} where yxpthdsqm = ? ";
                sql = sql.replace("#{tableName}",ledgerTableName);
                log.info("sql:"+sql);
                rs.executeQuery(sql,actAuthCode);
                if (rs.next()) {
                    String APPROVED_AT = Util.null2String(rs.getString("APPROVED_AT"));
                    result.put("APPROVED_AT", APPROVED_AT);
                }else {
                    result.put("APPROVED_AT", "");
                }
            }else {
                throw new Exception("请明确来源系统，现只支持创新结算系统和营销项目管理平台");
            }
            return result;
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
    }

    @Override
    public void externalUpdateInfo(String gs, String bm, String jbr, String sqzy, String htbm, String htmc, String projectCode, String projectName, String xmlxbm, String xmlxmc, String gysbm, String gysmc, String yxpthdsqm, String kczje) throws Exception {
        try {
            // 外部出资活动台账表单id
            int formId = Integer.parseInt(PropUtils.BB_LOG.getPropValue("marketSubject", "external_funding_activity_form_id"));
            // 外部出资活动台账模块id
            int formmodeId = Integer.parseInt(PropUtils.BB_LOG.getPropValue("marketSubject", "external_funding_activity_formmode_id"));
            // 获取外部出资活动台账主表表名
            String maintablename = FormDataUtils.getTableName(formId);
            log.info("获取外部出资活动台账主表表名："+maintablename);
            log.info("UID：" + user.getUID()+",Type："+user.getType());
            if(maintablename != null && !"".equals(maintablename)){
                List<JSONObject> list = FormDataUtils.selectInfoByProjectCode(maintablename, yxpthdsqm);
                log.info("外部出资活动台账数据id:"+list);
                if(list.size() > 0){
                    String id = list.get(0).getString("id");
                    FormDataUtils.updateInfoById(maintablename, id, gs, bm, jbr, sqzy, htbm, htmc, projectName, xmlxbm, xmlxmc, gysbm, gysmc, yxpthdsqm, kczje);
                }else {
                    FormDataUtils.addExternalInfoBySth(formId,formmodeId,maintablename,user,gs,bm,jbr,sqzy,htbm,htmc,projectCode,projectName,xmlxbm,xmlxmc,gysbm,gysmc,yxpthdsqm,kczje);
                }
            }else {
                throw new Exception("获取外部出资活动台账数据库表名异常！");
            }
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
    }



}
